<?php
class PostsController extends AppController {

    public $name = 'Posts';
    public $helpers = array('Time','Number','Media.Medium');

    public function beforeFilter(){
        parent::beforeFilter();
        $this->Auth->allow('view','download');
    }

    public function index(){
        $this->Post->recursive = 0;
        $this->set('posts', $this->paginate());
    }

    public function view($id = null){
        if(!$id){
            $this->Session->setFlash(__('Invalid Post.', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->Post->updateClicks($id);
        $this->Post->recursive = 1;
        $this->set('post', $this->Post->read(null, $id));
    }

    public function add(){
        if(!empty($this->data)){
            $this->Post->create();
            $this->data['Post']['user_id'] = $this->user['id'];
            if($this->Post->save($this->data)){
                $this->Session->setFlash(__('The Post has been saved', true));
                $this->redirect(array('action'=>'edit', $this->Post->getLastInsertId()));
            } else {
                $this->Session->setFlash(__('The Post could not be saved. Please, try again.', true));
            }
        }
        $users = $this->Post->User->find('list');
        $postTerms = $this->Post->PostTerm->find('list');
        $this->set(compact('users', 'postTerms'));
    }

    public function edit($id = null){
        if(!$id && empty($this->data)){
            $this->Session->setFlash(__('Invalid Post', true));
            $this->redirect(array('action'=>'index'));
        }
        if(!empty($this->data)){
            //debug($this->data);
            if($this->Post->saveAll($this->data, array('validate'=>'first'))){
                $this->Session->setFlash(__('The Post has been saved', true));
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash(__('The Post could not be saved. Please, try again.', true));
            }
        }
        if(empty($this->data)){
            $this->Post->recursive = 1;
            $this->data = $this->Post->read(null, $id);
        }
        $postTerms = $this->Post->PostTerm->find('list');
        $this->set(compact('postTerms','id'));
    }

    public function delete($id = null){
        if(!$id){
            $this->Response->json(false, __('Invalid id for Post', true));
        }
        if($this->Post->del($id)){
            $this->Response->json(true, __('Post deleted', true));
        }
    }
    public function download($id=null){
        if(!$id){
            $this->Session->setFlash(__('Invalid Attachment', true));
            $this->redirect(array('action'=>'index'));
        }
        $attachment = $this->Post->get_attachment($id);
        // Redirect to home page if invalid attachment data returned
        if(!$attachment || empty($attachment['Attachment']['id'])){
            $this->Session->setFlash(__('Invalid Attachment', true));
            $this->redirect(array('action'=>'index'));
        }
        // Response with Component.Response.File
        $attachment = $attachment['Attachment'];
        $pieces = explode('.', $attachment['basename']);
        $extension = $pieces[count($pieces)-1];
        $params = array(
            'name'      => empty($attachment['alternative']) ? $attachment['basename'] : $attachment['alternative'],

            'size'      => $attachment['size'],
            'extension' => $extension,
            'file'      => MEDIA . $attachment['dirname'] . DS . $attachment['basename'],
            'mimeType'  => $attachment['mime_type']
        );
        $this->Response->file($params);
    }

    public function search(){}

}
?>